//
//  QJLoginVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJLoginVC.h"
#import "QJRegistVC.h"
#import "QJForgetVC.h"

#import "WWNetworkHelper.h"
@interface QJLoginVC ()

@end

@implementation QJLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"登录";
    [self setupLoginViewAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    
    // 设置键盘
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //控件不被导航栏遮挡
//    self.navigationController.navigationBar.translucent = YES;
}
#pragma mark -- 界面布局
- (void)setupLoginViewAction{
    UIButton *rightButton = [[UIButton alloc] init];
    [rightButton setTitleColor:HexRGB(0x29b5de) forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    rightButton.frame = CGRectMake(0, 0, 80, 30);
    [rightButton setTitle:@"注册" forState:UIControlStateNormal];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [rightButton addTarget:self action:@selector(rightItemClicked) forControlEvents:UIControlEventTouchUpInside];
    
    self.phoneTF.clearButtonMode = UITextFieldViewModeAlways;
    self.secrectTF.clearButtonMode = UITextFieldViewModeAlways;
    
    //再次编辑就清空
    self.secrectTF.clearsOnBeginEditing = YES;
    
//    self.phoneView.layer.borderWidth = 0.5;
//    self.phoneView.layer.borderColor = HexRGB(0x29b5de).CGColor;
//    self.phoneView.layer.cornerRadius = 2;
//    self.phoneView.layer.masksToBounds = YES;
//
//    self.secrectView.layer.borderWidth = 0.5;
//    self.secrectView.layer.borderColor = HexRGB(0x29b5de).CGColor;
//    self.secrectView.layer.cornerRadius = 2;
//    self.secrectView.layer.masksToBounds = YES;
}
#pragma mark -- 忘记密码
- (IBAction)forgetBtnClickAction:(UIButton *)sender {
    QJForgetVC *VC = [[QJForgetVC alloc]init];
    KPushViewControllerAction(VC);
}
#pragma mark -- 登录
- (IBAction)loginBtnClickAction:(UIButton *)sender {

//    // 测试
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    [dict setObject:@"18120557020" forKey:@"username"];
//    [dict setObject:@"123456" forKey:@"password"];
//    [dict setObject:@"123456" forKey:@"password2"];
//
////    NSString *url = @"http://www.quanjieshop.com/api/user/do_login";
//
//    [WWNetRequest fetchLoginWithParameters:dict success:^(id response) {
//        NSLog(@"登录成功 resp = %@", response);
//
//        NSDictionary *data = response[@"data"];
//
//        [MBPHUD showAlertWithTitle:@"登录成功!"];
//        UserInfoModel *model = [[UserInfoModel alloc]initWithDictionary:data error:nil];
//        [QJUserInfo saveCurrentUser:model];
//        KPopViewControllerAction;
//
//    } failure:^(NSDictionary *error) {
//        NSLog(@"登录失败 error = %@", error);
//
//    }];
//    // 测试结束
    
    if (self.phoneTF.text.length != 11) {
        [MBPHUD showAlertWithTitle:@"您输入的手机号格式错误!"];
    }else if (self.secrectTF.text.length < 6){
        [MBPHUD showAlertWithTitle:@"您输入的密码不足6位!"];
    }else if (self.secrectTF.text.length > 16){
        [MBPHUD showAlertWithTitle:@"您输入的密码超过16位!"];
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:self.phoneTF.text forKey:@"username"];
        [dict setObject:self.secrectTF.text forKey:@"password"];
        
        [WWNetRequest fetchLoginWithParameters:dict success:^(id response) {
            NSLog(@"登录成功 response = %@", response);
            if (response) {
                NSDictionary *data = response[@"data"];
                
                [MBPHUD showAlertWithTitle:@"登录成功!"];
                UserInfoModel *model = [[UserInfoModel alloc]initWithDictionary:data error:nil];
                [QJUserInfo saveCurrentUser:model];
                KPopViewControllerAction;
            }
            
        } failure:^(NSDictionary *error) {
            
        }];
    }
}
#pragma mark -- 注册
- (void)rightItemClicked{
    QJRegistVC *VC = [[QJRegistVC alloc]init];
    KPushViewControllerAction(VC);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
